<?php
/**
 * Created by PhpStorm OF yuyi.
 * User: YUYI
 * Date: 2018/5/8 0008
 * Time: 下午 5:18
 * Administrator
 */

namespace app\install\controller;


use app\common\controller\Base;
use think\Exception;
use think\File;

class Index extends Base
{
    protected $isPublic = [
        'index','install','installTestDBConnect'
    ];

    public function index(){
        if (is_file(ROOT_PATH.'install.lock')){
            $this->error('程序已被安装。');
        }
        return $this->fetch();
    }

    public function install()
    {
        if (is_file(ROOT_PATH.'install.lock')){
            $this->error('程序已被安装。');
        }
        $dataFile = __DIR__.DS.'../../database.php';
        if (is_file($dataFile)){
            $r = unlink($dataFile);
            trace($r,'delete result');
        }
        $data = require __DIR__.DS.'../../database.example.php';
        $data['hostname'] = input('hostname')?:'127.0.0.1';
        $data['database'] = input('database')?:'vovoqi';
        $data['username'] = input('username')?:'root';
        $data['password'] = input('password')?:'root';
        $data['hostport'] = input('hostport')?:'3306';
        $str = "<?php\n
// +----------------------------------------------------------------------\n
// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n
// +----------------------------------------------------------------------\n
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.\n
// +----------------------------------------------------------------------\n
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n
// +----------------------------------------------------------------------\n
// | Author: liu21st <liu21st@gmail.com>\n
// +----------------------------------------------------------------------\n
        \nreturn ";
        $str .= $this->arrToText($data).";";
        $f = new File($dataFile,'w+');
        $r = $f->fwrite($str);
        $lock = new File(ROOT_PATH.'install.lock','w+');
        $lock->fwrite("true");
        $sql = file_get_contents(ROOT_PATH.'vovoqi.sql');
        try{
            $mysqli = new \mysqli($data['hostname'], $data['username'], $data['password'], $data['database'], $data['hostport']);
            if ($mysqli->connect_error) {
                return $this->errorInfo('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
            }
            if (mysqli_connect_error()) {
                return $this->errorInfo('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
            }
            $r = $mysqli->query($sql);
            $mysqli->error?$this->assign('error',$mysqli->error):$this->assign('success',1);
            $mysqli->close();
            if ($r)$this->assign('success',1);
            return $this->fetch();
        }catch (Exception $exception){
            $this->assign('error',$exception->getMessage());
            $this->assign('sql',ROOT_PATH.'vovoqi.sql');
            return $this->fetch();
        }
    }

    /**
     * 数组转字符串
     * @param $arr
     * @return bool|string
     */
    public function arrToText($arr)
    {
        if (!is_array($arr)){
            return false;
        }
        $str ="\t". '['."\n";
        foreach ($arr as $key=>$item){
            if (!is_array($item)){
                $str .= '"'.$key.'"'.'=>'.'"'.$item.'"'.",\n\t";
            }else{
                $str .= $this->arrToText($item).",\n\t";
            }
        }
        $str.="]";
        return $str;
    }


    public function installTestDBConnect()
    {
        if (!$this->request->isAjax() || !$this->request->isPost()){
            $this->error('非法访问');
        }
        $data['hostname'] = input('hostname')?:'127.0.0.1';
        $data['database'] = input('database')?:'vovoqi';
        $data['username'] = input('username')?:'root';
        $data['password'] = input('password')?:'root';
        $data['hostport'] = input('hostport')?:'3306';
        try {
            $mysqli = new \mysqli($data['hostname'], $data['username'], $data['password'], $data['database'], $data['hostport']);
            if ($mysqli->connect_error) {
                return $this->errorInfo('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
            }
            if (mysqli_connect_error()) {
                return $this->errorInfo('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
            }
            $mysqli->close();
        }catch (Exception $exception){
            return $this->exceptionInfo($exception);
        }
        return $this->successInfo('连接成功！');
    }
}